<template>
  <el-card>
    <el-row>
      <el-col :span="18">
        <el-form class="yangshi" size="small" :model="ruleForm">
          <el-form-item label="关键字" label-width="80px">
            <el-input
              v-model="ruleForm.keyword"
              placeholder="根据文章标题搜索"
              autocomplete="off"
              type="text"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="80px">
            <el-select
              v-model="ruleForm.state"
              style="display: block; width: 100%"
              placeholder="请选择"
            >
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clear">清除</el-button>
            <el-button type="primary" size="small" @click="getList"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="6">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="small"
          style="float: right"
          @click="update"
          >新增技巧</el-button
        >
      </el-col>
    </el-row>
    <el-alert
      :title="`数据一共${data.counts}行`"
      type="info"
      style="margin-bottom: 20px"
      :closable="false"
      show-icon
    ></el-alert>
    <el-table
      :data="data.items"
      :header-cell-style="{ backgroundColor: '#fafafa' }"
    >
      <el-table-column label="序号" prop="id"> </el-table-column>
      <el-table-column label="文章标题" prop="title"> </el-table-column>
      <el-table-column label="阅读数" prop="visits"> </el-table-column>
      <el-table-column label="录入人" prop="username"> </el-table-column>
      <el-table-column label="录入时间">
        <template slot-scope="{ row }">
          {{ row.createTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{ row }">
          {{ row.state === 0 ? '已禁用' : '已启用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @click="see(row)">预览</el-button>
          <el-button type="text" @click="disable(row.id, row.state)">
            {{ row.state === 0 ? '启用' : '禁用' }}
          </el-button>
          <el-button type="text" :disabled="!!row.state" @click="update(row)"
            >修改</el-button
          >
          <el-button type="text" :disabled="!!row.state" @click="del(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ruleForm.page"
      :page-sizes="[3, 5, 10]"
      :page-size="ruleForm.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="data.counts"
    >
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="isDialog">
      <el-form
        ref="dialogFormRef"
        :model="dialogForm"
        :rules="rules"
        label-width="100px"
        style="width: 100%; margin-right: 20px"
      >
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="dialogForm.title"></el-input>
        </el-form-item>

        <el-form-item label="文章内容：" prop="articleBody">
          <editor-bar
            @blur="context"
            :catchData="catchData"
            :content="editorContent"
          ></editor-bar>
        </el-form-item>
        <el-form-item label="视频地址：">
          <el-input v-model="dialogForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="预览文章" :visible.sync="previewDialog">
      <div class="previewContext">
        <h2>{{ seeData.title }}</h2>
        <div>
          <span>{{ seeData.time | formatTime }}</span>
          <span>{{ seeData.username }}</span>
          <span class="el-icon-view"></span>
          <span>{{ seeData.visits }}</span>
        </div>
      </div>
      <div class="richText">123</div>
    </el-dialog>
  </el-card>
</template>

<script>
import EditorBar from '@/module-hmmm/components/EditorBar'
import { list, add, changeState, update, remove } from '@/api/hmmm/articles.js'
import dayjs from 'dayjs'
export default {
  components: {
    EditorBar
  },
  filters: {
    // 格式化入职日期的方法
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  data () {
    return {
      dialogTitle: '新增文章',
      previewDialog: false,
      isDialog: false,
      ruleForm: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      dialogForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      data: {},
      seeData: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    close () {
      this.dialogForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.dialogFormRef.resetFields()
      this.isDialog = false
    },
    // 删除用户
    async del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
      await remove(id)
      this.getList()
    },
    // 弹层表单提交
    async submit () {
      await this.$refs.dialogFormRef.validate()
      if (this.dialogForm.id) {
        await update(this.dialogForm)
      } else {
        await add(this.dialogForm)
      }
      this.$message.success('操作成功')
      this.getList()
      this.dialogForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      await this.$refs.dialogFormRef.resetFields()
      this.isDialog = false
    },
    // 编辑修改
    update (row) {
      // console.log(id)
      if (row.id) {
        this.dialogTitle = '编辑文章'
        this.dialogForm = row
      } else {
        this.dialogTitle = '新增文章'
      }
      this.isDialog = true
    },
    // 禁用启用
    async disable (id, state) {
      console.log('xfghjkl', id, state)
      await changeState({ id: id, state: state === 0 ? 1 : 0 })
      this.$message.success('操作成功')
      this.getList()
    },
    see (row) {
      // console.log(row)
      this.previewDialog = true
      this.seeData = row
    },
    // 富文本校验
    context () {
      this.$refs.dataForm.validateField('articleBody')
    },
    // pageSize每页显示条目个数 改变时会触发
    handleSizeChange (val) {
      this.ruleForm.pagesize = val
      this.getList()
    },
    // currentPage当前页数 改变时会触发
    handleCurrentChange (val) {
      this.ruleForm.page = val
      this.getList()
    },
    clear () {
      this.ruleForm = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
    },
    async getList () {
      const { data } = await list(this.ruleForm)
      this.data = data
      // console.log(data)
    },
    // 监听富文本的输入
    catchData (e) {
      // console.log('e=====?>', e)
      this.dialogForm.articleBody = e
    },
    // 富文本中的内容
    editorContent (e) {
      console.log('ee=====?>', e)
    }
  }
}
</script>

<style scoped>
.el-card {
  margin: 20px;
}
.yangshi .el-form-item {
  display: inline-block;
  margin-bottom: 18px;
  vertical-align: top;
  margin-right: 10px;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
}
.previewContext {
  border-bottom: 1px dashed #ccc;
  color: #606266;
}
.previewContext h2 {
  padding: 0;
  margin: 0;
}
.previewContext div {
  margin: 10px 0 10px 10px;
}
.previewContext div span {
  margin-left: 10px;
  font-size: 14px;
}
.richText {
  height: 65px;
  line-height: 65px;
  background: #f5f5f5;
}
</style>
